MQTT_Receive block

Short summary

Name

MQTT_Receive

→POU type

→function

Category

more system blocks, MQTT

Graphical interface

Available since

  • version 1.28.0 (for Neuron Power Engineer) and version 2.3.1301 of the →runtime system – initial variant

  • version 1.108.0 (for Neuron Power Engineer) and version 3.18.0 of the →runtime system – enhancement: return value with data type  STRING (instead of the previous input topic); data type MQTT_RC for output rc (instead of DINT)

(warning) This block is supported for →Raspberry Pi

Functionality

The block pops one message from the receive buffer (a circular buffer) associated with the given connection handler.

(info) The data is transferred by means of an already existing MQTT broker (see "Preparing/Realizing data transfer via MQTT").

Inputs, outputs, return value

 

Identifier

→Data type

Description

Inputs: 

ch

DINT

connection handler (as obtained by the MQTT_Connect block)

payload

REF_TO BYTE

buffer where received payload can be stored

payload_len

DINT

size of the payload buffer in bytes

Outputs:

len

DINT

length of the received payload in bytes

 

rc

MQTT_RC

return code of involved MQTT functions as specified in data type MQTT_RC

Return value:

STRING

returns the topic of the received message

Input EN and output ENO are available when →calling the block. See "Execution control: EN, ENO" for information on input EN and output ENO.

See:

Example for usage within ST-editor

Program with selected calls of MQTT blocks
PROGRAM Program2
    VAR
        ioImageRemote : ARRAY[0..1023] OF BYTE;
        ioImageRemoteSize : DINT := 1024;
        ioImageRemoteLen : DINT := -1;
        topic : STRING[255];
        ch : DINT := -1;
        cnt : DINT := 0;
        rc : MQTT_RC;
        state : MQTT_SUBSCRIBER_STATE := MQTT_SUBSCRIBER_STATE#INVALID;
          
        subscribed : BOOL := FALSE;
        message_received : BOOL := FALSE;
    END_VAR
     
    state := MQTT_GetState(ch := ch, rc => rc, ENO => ENO);
 
    IF state <> MQTT_SUBSCRIBER_STATE#CONNECTING AND state <> MQTT_SUBSCRIBER_STATE#CONNECTED THEN
        ch := MQTT_Connect(address := '192.168.1.107', clientId := 'RTS2', rc => rc, ENO => ENO);
    END_IF;
    IF state = MQTT_SUBSCRIBER_STATE#CONNECTED THEN
        /* connect to topic in case connection has been established */        
        IF NOT(subscribed) THEN
            MQTT_Subscribe(ch := ch, topic := 'sample', rc => rc);
            subscribed := rc = MQTT_RC#OK;
        END_IF;
          
        IF subscribed THEN
            /* if subscribed, obtain only the latest message */
            message_received := FALSE;
            REPEAT
                topic := MQTT_Receive(ch := ch,
                                payload := REF(ioImageRemote[0]),
                                payload_len := ioImageRemoteSize,
                                len => ioImageRemoteLen,
                                rc => rc);
                IF rc <> MQTT_RC#OK THEN
                    EXIT;
                END_IF;
                message_received := TRUE;
            UNTIL FALSE
            END_REPEAT;
        END_IF;
    ELSE
        /* if connection has been lost, subscription has been canceled as well*/
        subscribed := FALSE;
    END_IF;

When creating your application within the ST-editor, enter a call of a block by typing the text as requested by the syntax or use Content Assist.